import { VarInit } from "~/models/data/var_init";
import { ISlot } from "./graph/slot/instance";
import { PSlot } from "./graph/slot/prototype";
import { PSlotType } from "./graph/slot/type";
import type { DataType } from "./types/type";

export type Varlike = ISlot | VarInit | PSlot

export class VarlikeAdaptor {

  static receive(varlike: Varlike) {
    return ['PSlot', 'ISlot', 'VarInit'].includes(varlike.className)
  }

  constructor(
    public varlike: Varlike
  ){
    if (!VarlikeAdaptor.receive(varlike)) {
      console.error(varlike);
      throw new Error(`VarlikeAdaptor不支持此类型: ${varlike}`)
    }
  }

  get isISlot() { return this.varlike.className === 'ISlot' }
  get isPSlot() { return this.varlike.className === 'PSlot' }
  get isVar() { return this.varlike.className === 'VarInit' }

  get type(): DataType {
    if (this.isISlot) { return (this.varlike as ISlot).prototype.dataType! }
    else if (this.isPSlot) { return (this.varlike as PSlot).dataType! }
    else { return (this.varlike as VarInit).type }
  }

  set type(t: DataType)  {
    if (this.isISlot) { (this.varlike as ISlot).prototype.dataType = t }
    else if (this.isPSlot) { (this.varlike as PSlot).dataType = t }
    else { (this.varlike as VarInit).type = t }
  }

  get value(): any {
    if (this.isISlot) { return (this.varlike as ISlot).data?.content }
    else if (this.isPSlot) { return (this.varlike as PSlot).defaultValue?.content }
    else { return (this.varlike as VarInit).initial }
  }

  set value(v: any) {
    if (this.isISlot) { (this.varlike as ISlot).data!.content = v }
    else if (this.isPSlot) { (this.varlike as PSlot).defaultValue!.content = v }
    else { (this.varlike as VarInit).initial = v }
  }

  get name(): string {
    if (this.isISlot) { return (this.varlike as ISlot).prototype.name }
    else if (this.isPSlot) { return (this.varlike as PSlot).name }
    else { return (this.varlike as VarInit).name }
  }

  set name(n: string) {
    if (this.isISlot) { (this.varlike as ISlot).prototype.name = n }
    else if (this.isPSlot) { (this.varlike as PSlot).name = n }
    else { (this.varlike as VarInit).name = n }
  }
}